package org.catdroid.android.farmalona;

import java.io.FileNotFoundException;
import java.io.IOException;
import org.catdroid.android.farmalona.bean.Pharmacy;
import org.catdroid.android.farmalona.utils.Utils;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PharmacyListActivity extends ListActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setTitle("Farmacias de Barcelona");
		try {setListAdapter(new SimpleArrayAdapter(this));} 
		catch (FileNotFoundException e) {e.printStackTrace();} 
		catch (IOException e) {e.printStackTrace();} 
		catch (Exception e) {e.printStackTrace();}

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(),"Click!!", Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	SimpleArrayAdapter adapter = null;
	class SimpleArrayAdapter extends ArrayAdapter {
		
		Context context = null;
		
		public SimpleArrayAdapter(Context c) throws Exception {
			super(c, R.layout.activity_pharmacylist,Utils.getListPharmacyFromCSV(getResources().openRawResource(R.raw.farmacias2)));
			context = c;
		}
		
	    public View getView(int pos, View convertView, ViewGroup parent) {
	    	
	    	ViewHolder holder = null;
	    	if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
		        LayoutInflater mInflater = LayoutInflater.from(context);
                convertView = mInflater.inflate(R.layout.activity_pharmacylist, null);
                // Creates a ViewHolder and store references to the two children views we want to bind data to.
                holder = new ViewHolder();
                holder.codiCapa = (TextView) convertView.findViewById(R.id.codiCapa);
                holder.nomCapaCat = (TextView) convertView.findViewById(R.id.nomCapaCat);
                holder.equipament = (TextView) convertView.findViewById(R.id.equipament);
                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            Pharmacy pharmacy = new Pharmacy();
			try {pharmacy = getPharmacy(pos);} 
			catch (FileNotFoundException e) {e.printStackTrace();} 
			catch (IOException e) {e.printStackTrace();} 
			catch (Exception e) {e.printStackTrace();}
			
            holder.codiCapa.setText(pharmacy.codiCapa);
            holder.nomCapaCat.setText(pharmacy.nomCapaCat);
            holder.equipament.setText(pharmacy.equipament);
	    	return convertView;
	    }
	    
	    private class ViewHolder {
            TextView codiCapa;
            TextView nomCapaCat;
            TextView equipament;
        }
	    
	    private Pharmacy getPharmacy(int pos) throws Exception{
	    	Pharmacy pharmacy = Utils.getListPharmacyFromCSV(getResources().openRawResource(R.raw.farmacias)).get(pos);
	    	return pharmacy;
	    }
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
}